import Vue from "vue";
import VueRouter from "vue-router";
import store from '../store/index'; 
//登陆页 
const login = () =>import( "@/views/login/index.vue");
//主页 
const home = () =>import( "@/views/home/index.vue"); //主页     这种import方式引用为按需加载,大大提升首屏速度 不需要就不加载
//解决路由重复点击报错
const originalPush = VueRouter.prototype.push;      
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
//解决路由重复点击报错
Vue.use(VueRouter);
const routes = [
  // 底部
  {
    path: "/home",
    name: "home",
    component: home,
  },
  {
    path: "/cate",
    name: "cate",
    component: () =>import( "@/views/cate/index.vue"),
  },
  {
    path: "/active",
    name: "active",
    component: () =>import( "@/views/active/index.vue"),
  },
  {
    path: "/health",
    name: "health",
    component: () =>import( "@/views/health/index.vue"),
  },
  {
    path: "/my",
    name: "my",
    component: () =>import( "@/views/my/index.vue"),
  },
  // 底部
  // 抽奖
  {
    path: "/prizeRecords",
    name: "prizeRecords",
    component: () =>import( "@/views/luckdraw/prizeRecords.vue"),
  },
  // 中奖记录
  {
    path: "/luckdraw",
    name: "luckdraw",
    component: () =>import( "@/views/luckdraw/index.vue"),
  },
  {
    path: "/",
    name: "/",
    component: login,
    meta: {
      isAuth: false
    }
  },
  {
    path: "/login",
    name: "login",
    component: login,
    meta: {
      isAuth: false
    }
  },
  {
    path: "/404",
    name: "404",
    component: () =>import( "@/views/error/404.vue"),
    meta: {
      isAuth: false
    }
  },
  {
    path: "/503",
    name: "503",
    component: () =>import( "@/views/error/503.vue"),
    meta: {
      isAuth: false
    }
  },
 
];

const router = new VueRouter({
  routes
});
// 路由守卫 
router.beforeEach((to, form, next) => {
  if(to.meta&&to.meta.title){
    document.title=to.meta.title        //设置页面标题 不需要设置不同页面标题可删除
  }else{
    document.title='琪迹养生'
  }
  if (to.meta && to.meta.isAuth === false) { // 判断该路由是否需要登录权限 false 为不需要登录
    next();
  } else {
    if (store.state.login) {  
      next();
    } else {
      document.title='琪迹养生'
      // next({ path: '/login' });
      next();
    }
  }
 
});
export default router;
